/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.m3d.texture.v2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.meanzoft.m3d.resource.ResourceManager;
import org.meanzoft.m3d.texture.v2.decoder.PNGDecoder;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Meanz
 */
public class TextureCreator {

    /** The colour model for the GL image */
    private static ColorModel glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
            new int[]{8, 8, 8, 8},
            true,
            false,
            ComponentColorModel.TRANSLUCENT,
            DataBuffer.TYPE_BYTE);
    ;
    
    private static ColorModel glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
            new int[]{8, 8, 8, 0},
            false,
            false,
            ComponentColorModel.OPAQUE,
            DataBuffer.TYPE_BYTE);

    public ByteBuffer imageToBuffer(BufferedImage bufferedImage, int width, int height) {
        ByteBuffer imageBuffer = null;
        WritableRaster raster;
        BufferedImage texImage;

        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable());

        // copy the source image into the produced image

        Graphics g = texImage.getGraphics();
        g.setColor(new Color(0f, 0f, 0f, 0f));
        g.fillRect(0, 0, width, height);
        g.drawImage(bufferedImage, 0, 0, null);

        // build a byte buffer from the temporary image

        // that be used by OpenGL to produce a texture.

        byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();

        imageBuffer = ByteBuffer.allocateDirect(data.length);
        imageBuffer.order(ByteOrder.nativeOrder());
        imageBuffer.put(data, 0, data.length);
        imageBuffer.flip();

        return imageBuffer;
    }

    public BufferedImage bufferToImage(ByteBuffer imageBuffer, int width, int height) {
        return null;
    }

    public Texture loadFile(String fileName) {
        Texture texture = Texture.create();
        texture.setId(ResourceManager.getManager().getTextureLoader().createTextureID());
        texture.bind();

        if (fileName.endsWith(".png")
                || fileName.endsWith(".jpg")
                || fileName.endsWith(".bmp")) {

            File imgFile = new File(fileName);
            if (!imgFile.exists()) {
                System.err.println("Tried to load texture that doesn't exist.");
                return null;
            }

            BufferedImage image = null;

            try {
                image = ImageIO.read(new File(fileName));
            } catch (IOException iex) {
                iex.printStackTrace();
                return null;
            }
            ByteBuffer imageData = imageToBuffer(image,
                    image.getWidth(),
                    image.getHeight());
            texture.setImageData(imageData);
            texture.build(
                    image.getColorModel().hasAlpha()
                    ? GL_RGBA : GL_RGB, GL_RGBA);
        }
        return texture;
    }

    public Texture combineTextures(Object... params) throws Exception {
        if (params.length < 2) {
            System.err.println("Invalid params for combineTextures");
            return null;
        }
        int width = 0, height = 0;
        Texture texture = Texture.create();
        texture.setId(ResourceManager.getManager().getTextureLoader().createTextureID());
        texture.bind();
        Texture[] textures = new Texture[params.length / 3];
        for (int i = 0; i < params.length / 3; i++) {
            textures[i] = (Texture) params[i * 3];
            width += textures[i].getWidth();
            height += textures[i].getHeight();
        }

        texture.setWidth(width);
        texture.setHeight(height);

        byte[] imageBuffer = new byte[width * height];

        for (int i = 0; i < params.length / 3; i++) {

            for (int x = 0; x < textures[i].getWidth(); x++) {
                for (int y = 0; y < textures[i].getHeight(); y++) {
                    int texX = 0;//(int) params[i * 3 + 1];
                    int texY = 0;//(int) params[i * 3 + 2];
                    imageBuffer[texX + x + (y * width)] = (byte)253;
                }
            }
        }

        ByteBuffer buffer = ByteBuffer.allocateDirect(width * height);
        buffer.order(ByteOrder.nativeOrder());
        buffer.put(imageBuffer);
        buffer.flip();

        texture.setImageData(buffer);
        texture.build(GL_RGBA, GL_RGBA);
        return texture;
    }
}
